﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using FirebirdSql.Data.FirebirdClient;
using System.Security.Cryptography;
using System.Globalization;

namespace RMSM
{
    public static class StaticUtil
    {
        #region getvalue
        public static string GetString(object value)
        {
            if (value == null)
                return "";
            if (value is DBNull)
                return "";
            return value.ToString();
        }

        public static string GetString(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            else
                return value;
        }

        public static string GetString(object value, string valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            if (value is DBNull)
                return valueWhenNull;
            return value.ToString();
        }

        public static bool GetBool(object value)
        {
            return GetBool(value, false);
        }

        public static bool GetBool(object value, bool valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            else
                if (value is DBNull)
                    return valueWhenNull;
                else
                    return (bool)value;
        }

        public static bool GetBool(bool? value)
        {
            return GetBool(value, false);
        }

        public static bool GetBool(bool? value, bool valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            else
                return value.Value;
        }

        public static bool? GetRealBool(object value)
        {
            if (value == null)
                return null;
            else
                if (value is DBNull)
                    return null;
                else
                    return Convert.ToBoolean(value);
        }

        public static int GetInt(object value)
        {
            return GetInt(value, 0);
        }

        public static int GetInt(object value, int valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            if (value is DBNull)
                return valueWhenNull;

            int i = 0;
            try
            {
                i = Convert.ToInt32(value);
            }
            catch
            {
                return valueWhenNull;
            }

            return i;
        }

        public static int GetInt(int? value)
        {
            return GetInt(value, 0);
        }

        public static int GetInt(int? value, int valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            else
                return value.Value;
        }

        public static int? GetRealInt(object value)
        {
            return GetRealInt(value, null);
        }

        public static int? GetRealInt(object value, int? valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            if (value is DBNull)
                return valueWhenNull;

            int i = 0;
            try
            {
                i = Convert.ToInt32(value);
            }
            catch
            {
                return null;
            }
            return i;
        }

        public static double GetDouble(double? value)
        {
            return GetDouble(value, 0);
        }

        public static double GetDouble(double? value, double valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            else
                if (double.IsInfinity(value.Value))
                    return valueWhenNull;
                else
                    return value.Value;
        }

        public static double GetDouble(object value)
        {
            return GetDouble(value, 0);
        }

        public static double GetDouble(object value, double valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            if (value is DBNull)
                return valueWhenNull;

            try
            {
                double d = Convert.ToDouble(value);
                if (double.IsInfinity(d))
                    return valueWhenNull;
                else
                    return d;
            }
            catch
            {
                return valueWhenNull;
            }
        }

        public static double? GetRealDouble(object value)
        {
            return GetRealDouble(value, null);
        }

        public static double? GetRealDouble(object value, double? valueWhenNull)
        {
            if (value == null)
                return null;
            if (value is DBNull)
                return null;
            return GetDouble(value);
        }

        public static DateTime GetDateTime(object value, DateTime valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            if (value is DBNull)
                return valueWhenNull;

            DateTime i = DateTime.MinValue;
            try
            {
                i = Convert.ToDateTime(value);
            }
            catch
            {
                return valueWhenNull;
            }
            return i;
        }

        public static DateTime GetDateTime(object value)
        {
            return GetDateTime(value, DateTime.MinValue);
        }

        public static DateTime GetDateTime(DateTime? value)
        {
            return GetDateTime(value, DateTime.MinValue);
        }

        public static DateTime GetDateTime(DateTime? value, DateTime valueWhenNull)
        {
            if (value == null)
                return valueWhenNull;
            else
                return value.Value;
        }

        public static DateTime? GetRealDateTime(object value)
        {
            if (value == null)
                return null;
            if (value is DBNull)
                return null;
            return Convert.ToDateTime(value);
        }

        public static string GetTextDateTime(DateTime? dt)
        {
            if (dt == null)
                return null;
            else
                return dt.Value.ToString("dd/MM/yyyy");
        }

        public static string GetSoTienStr(double? value)
        {
            if (value == null)
                return "";
            else
                if (value.Value == 0)
                    return "0";
                else
                    return value.Value.ToString("#,#");
        }

        public static string GetIntStr(int? value)
        {
            if (value == null)
                return "";
            else
                return value.Value.ToString();
        }
        #endregion getvalue

        #region compare
        public static bool IsEqual(string v1, string v2)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            return (v1 == v2);
        }

        public static bool IsEqual(bool? v1, bool? v2)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            return (v1.Value == v2.Value);
        }

        public static bool IsEqual(int? v1, int? v2)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            return (v1.Value == v2.Value);
        }

        public static bool IsEqual(double? v1, double? v2)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            return (v1.Value == v2.Value);
        }

        public static bool IsEqual(long? v1, long? v2)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            return (v1.Value == v2.Value);
        }

        public static bool IsEqual(DateTime? v1, DateTime? v2, bool dayOnly)
        {
            if (v1 == null && v2 == null)
                return true;

            if (v1 != null && v2 == null)
                return false;

            if (v1 == null && v2 != null)
                return false;

            string s1 = v1.Value.ToString((dayOnly ? "yyyyMMdd" : "yyyyMMddHHmm"));
            string s2 = v2.Value.ToString((dayOnly ? "yyyyMMdd" : "yyyyMMddHHmm"));

            return (s1 == s2);
        }
        #endregion compare

        #region getString
        public static string GetStr(string v)
        {
            if (v == null)
                return "";

            return v;
        }

        public static string GetStr(bool? v)
        {
            if (v == null)
                return "";

            return (v.Value ? "true" : "false");
        }

        public static string GetStr(int? v)
        {
            if (v == null)
                return "";

            return v.Value.ToString();
        }

        public static string GetStr(double? v, bool money)
        {
            if (v == null)
                return "";

            return (money ? v.Value.ToString("#,#") : v.Value.ToString());
        }

        public static string GetStr(long? v)
        {
            if (v == null)
                return "";

            return v.Value.ToString();
        }

        public static string GetStr(DateTime? v, bool dayOnly)
        {
            if (v == null)
                return "";

            return v.Value.ToString((dayOnly ? "dd/MM/yyyy" : "dd/MM/yyyy HH:mm"));
        }
        #endregion getString

        #region other
        public static DateTime RebuiltDateTime(DateTime dt, int hour, int min)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, hour, min, 0, 0);
        }

        public static bool Log_ValidColumn(string name)
        {
            return (name != "STT0" && name != "IsNew" && name != "IsDeleted" && name != "IsDirty" && name != "IsSavable" && name != "IsValid" && name != "BrokenRulesCollection" && name != "DELE" && name != "HstOldValue");
        }

        public static DateTime GetMinDay(DateTime? dt, DateTime minDay)
        {
            if (dt != null)
                if (minDay > dt.Value)
                    return dt.Value;

            return minDay;
        }

        public static DateTime GetMaxDay(DateTime? dt, DateTime maxDay)
        {
            if (dt != null)
                if (maxDay < dt.Value)
                    return dt.Value;

            return maxDay;
        }

        public static bool CompareDate(DateTime? dt1, DateTime? dt2)
        {
            if (dt1 == null || dt2 == null)
                return false;
            return (dt1.Value.ToString("yyyyMMdd") == dt2.Value.ToString("yyyyMMdd"));
        }

        public static bool IsEqual(object obj1, object obj2)
        {
            if (obj1 != null)
                if (obj1 is DBNull)
                    obj1 = null;

            if (obj2 != null)
                if (obj2 is DBNull)
                    obj2 = null;

            if (obj1 == null && obj2 == null)
                return true;

            if (obj1 == null && obj2 != null)
                return false;

            if (obj1 != null && obj2 == null)
                return false;

            return obj1.Equals(obj2);
        }

        public static string GetVarName(string value)
        {
            if (value == null)
                return "";

            if (value == "")
                return value;

            if (value.Length == 1)
                return value.ToLower();

            return value.Substring(0, 1).ToLower() + value.Substring(1);
        }

        public static string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }

        public static string[] GetDanhMuc(LoaiDanhMuc loaiDanhMuc)
        {
            switch (loaiDanhMuc)
            {
                case LoaiDanhMuc.None:
                    return new string[] { "None", "None" };
                default:
                    return new string[] { "", "" };
            }
        }

        public static string GetLoaiDanhMuc(LoaiDanhMuc loaiDanhMuc)
        {
            return GetDanhMuc(loaiDanhMuc)[0];
        }

        public static string RebuiltHoTen(string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;
            else
            {
                s = s.Trim();
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");

                string[] arrS = s.Split(' ');
                s = "";
                int i = 0;
                for (i = 0; i < arrS.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arrS[i]))
                    {
                        s += (s == "" ? "" : " ");
                        if (arrS[i].Length == 1)
                            s += arrS[i].ToUpper();
                        else
                            s += arrS[i].Substring(0, 1).ToUpper() + arrS[i].Substring(1).ToLower();
                    }
                }
            }

            return s;
        }

        public static string GetFilenameOnly(string s)
        {
            if (string.IsNullOrEmpty(s))
                return "";
            else
            {
                int i = s.LastIndexOf("\\");
                s = s.Substring(i + 1);
                return s;
            }
        }

        public static string UpperFirstCharacter(string s)
        {
            s = s.ToLower().Trim();
            string s1 = s.Substring(0, 1).ToUpper();
            s = s.Remove(0, 1);
            s = s.Insert(0, s1);
            return s;
        }

        public static int GetIntFromDT(DateTime? dt)
        {
            if (dt == null)
                return 0;
            else
                return dt.Value.Year * 10000 + dt.Value.Month * 100 + dt.Value.Day;
        }
        #endregion other

        #region DB
        public static FbDataReader ExecReader(string scmd)
        {
            if (string.IsNullOrEmpty(scmd))
                return null;

            FbConnection cnn = new FbConnection(Session.CnnStr);
            cnn.Open();

            FbCommand cmd = new FbCommand(scmd, cnn);
            return cmd.ExecuteReader();
        }

        public static object ExecScalar(string scmd)
        {
            if (string.IsNullOrEmpty(scmd))
                return null;

            object _return = null;

            try
            {
                using (FbConnection cnn = new FbConnection(Session.CnnStr))
                {
                    cnn.Open();

                    using (FbCommand cmd = new FbCommand(scmd, cnn))
                    {
                        _return = cmd.ExecuteScalar();
                    }
                }
            }
            catch
            {
            }

            return _return;
        }

        public static string ExecNonQuery(string scmd)
        {
            if (string.IsNullOrEmpty(scmd))
                return null;

            try
            {
                using (FbConnection cnn = new FbConnection(Session.CnnStr))
                {
                    cnn.Open();

                    using (FbCommand cmd = new FbCommand(scmd, cnn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return null;
        }
        #endregion DB

        #region Doi tien tu so sang chu
        public static string ConvertMoney2Str(decimal? number)
        {
            if (number == null)
                return "";

            string s = number.Value.ToString("#");
            string[] so = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            string[] hang = new string[] { "", "nghìn", "triệu", "tỉ" };
            int i, j, donvi, chuc, tram;
            string str = " ";
            bool booAm = false;
            decimal decS = 0;

            try
            {
                decS = Convert.ToDecimal(s.ToString());
            }
            catch
            {
            }

            if (decS < 0)
            {
                decS = -decS;
                s = decS.ToString();
                booAm = true;
            }

            i = s.Length;
            if (i == 0)
                str = so[0] + str;
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                    i--;
                    chuc = (i > 0 ? Convert.ToInt32(s.Substring(i - 1, 1)) : -1);

                    i--;
                    tram = (i > 0 ? Convert.ToInt32(s.Substring(i - 1, 1)) : -1);

                    i--;
                    if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                        str = hang[j] + str;

                    j++;
                    if (j > 3)
                        j = 1;

                    if ((donvi == 1) && (chuc > 1))
                        str = " mốt " + str;
                    else
                    {
                        if ((donvi == 5) && (chuc > 0))
                            str = " lăm " + str;
                        else
                            if (donvi > 0)
                                str = so[donvi] + " " + str;
                    }

                    if (chuc < 0)
                        break;
                    else
                    {
                        if ((chuc == 0) && (donvi > 0))
                            str = "lẻ " + str;

                        if (chuc == 1)
                            str = " mười " + str;

                        if (chuc > 1)
                            str = so[chuc] + " mươi " + str;
                    }

                    if (tram < 0)
                        break;
                    else
                        if ((tram > 0) || (chuc > 0) || (donvi > 0))
                            str = so[tram] + " trăm " + str;

                    str = " " + str;
                }
            }

            if (booAm)
                str = "Âm " + str;

            str = str.Trim();
            while (str.IndexOf("  ") >= 0)
                str = str.Replace("  ", " ");

            if (str != "")
            {
                str += " đồng";
                str = str.Substring(0, 1).ToUpper() + str.Substring(1);
            }
            return str;
        }

        public static string ConvertMoney2Str(string number)
        {
            decimal d = 0;
            try
            {
                d = Convert.ToDecimal(number);
                return ConvertMoney2Str(d);
            }
            catch
            {
                return "";
            }
        }

        public static string ConvertMoney2Str(double? number)
        {
            if (number == null)
                return "";

            return ConvertMoney2Str(Convert.ToDecimal(number));
        }
        #endregion Doi tien tu so sang chu

        #region encrype
        public static string Encrypte(string str)
        {
            TripleDES tripleDES = TripleDES.Create();
            tripleDES.IV = Encoding.ASCII.GetBytes("mn8jms0w");
            tripleDES.Key = Encoding.ASCII.GetBytes("hy673dsk0we9jfnif8encie9");
            tripleDES.Mode = CipherMode.CBC;
            tripleDES.Padding = PaddingMode.Zeros;

            ICryptoTransform ict = tripleDES.CreateEncryptor();
            byte[] data = Encoding.Unicode.GetBytes(str);
            byte[] enc = ict.TransformFinalBlock(data, 0, data.Length);
            return BitConverter.ToString(enc).Replace("-", "");
        }

        public static string Decrypte(string str)
        {
            if (String.IsNullOrEmpty(str))
                return "";

            TripleDES tripleDES = TripleDES.Create();
            tripleDES.IV = Encoding.ASCII.GetBytes("mn8jms0w");
            tripleDES.Key = Encoding.ASCII.GetBytes("hy673dsk0we9jfnif8encie9");
            tripleDES.Mode = CipherMode.CBC;
            tripleDES.Padding = PaddingMode.Zeros;

            int index = 0;
            byte[] data = new byte[str.Length / 2];
            int length = data.Length;

            try
            {
                for (index = 0; index < length; index++)
                    data[index] = byte.Parse(str.Substring(index * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);

                ICryptoTransform ict = tripleDES.CreateDecryptor();
                byte[] dec = ict.TransformFinalBlock(data, 0, length);
                return Encoding.Unicode.GetString(dec).Replace("\0", "");
            }
            catch
            {
                return "";
            }
        }
        #endregion encrype
    }
}
